require('./index.scss');
var Tool = require('common/tool');
require('comp/select');
require('comp/table');
require('comp/dialog');
var curRouterParam = stateman.current.param,
    page ;

var vm = avalon.define({
    $id: 'extractRecord',
    platform:"11",
    accountInfo:'',
    platformName:'',
    /* 账号列表 */
    accountDropdown:{
        checked:'',
        checkedText:'',
        list:[],
        changefn:function(el){
            vm.platformName = el.platformName;
            page.submit();
        }
    },
    /* 日期列表 */
    dateDropdown:{
        checked:'1',
        checkedText:'当日',
        list:[{
            val:"1",
            text:"当日",
        },{
            val:"2",
            text:"最近一周",
        },{
            val:"3",
            text:"最近一个月",
        },{
            val:"4",
            text:"最近三个月",
        }],
        changefn:function(el){
            vm.filterData.defaultETime = latelyDate(0);
            switch(el.val){
                case '1':
                    vm.filterData.defaultbTime = latelyDate(0);
                    break;
                case '2':
                    vm.filterData.defaultbTime = latelyDate(6);
                    break;
                case '3':
                    vm.filterData.defaultbTime = latelyMonth(1);
                    break;
                case '4':
                    vm.filterData.defaultbTime = latelyMonth(3);
                    break;
            }
            page.submit();
        }
    },
    /* 状态列表 */
    statusData:{
        checked:'',
        checkedText:'所有',
        list:[{
            val:"",
            text:"所有",
        },{
            val:"WITHDRAWAL_SUCCESS",
            text:"成功",
        },{
            val:"API_ERROR",
            text:"失败",
        },{
            val:"REJECTED",
            text:"已取消",
        },{
            val:"NOT_APPLICABLE",
            text:"不适用",
        }],
        changefn:function(){}
    },
    /* 查询条件 */
    filterData:{
        show:false,
        defaultbTime:'',
        defaultETime:'',
    },
    /* 查询结果的列表 */
    tb:{
        loading:'',
        totalPage:'',
        curr:1,                         //当前页码
        pageSize:20,                     //当前显示条数
        errMsg:'',
        jump:function(obj){
            if( obj.curr!=vm.tb.curr ){
                vm.tb.curr = obj.curr;
                page.submit();
            }
        },
        thList:[
            {label:'申请编号',text:function(v){
                return '<a href="javascript:;" class="pno" data-pno="'+v.pno+'">'+v.pno+'</a>';
            }},
            {label:'取款金额',text:function(v){
                return Number(v.transAmount).toFixed(2);
            }},
            {label:'申请时间',text:function(v){
                return Tool.timeZone(v.createDate);
            }},
            {label:'状态',text:"stateName"},
            {label:'更新时间',text:function(v){
                return v.withdrawalSettleTime ? Tool.timeZone(v.withdrawalSettleTime) : Tool.timeZone(v.updateDate);
            }}
        ],
        list:''
    },
    extractDialog:{
        loading:false,
        display:'none',
        title: '取款详情',
        statusArr:{
            '0':'申请已提交',
            '1':'审批中',
            '2':'审批通过',
            '3':'支付成功',
            '4':'支付失败',
            '-1':'申请已取消',
            '-2':'申请已取消',
            '-3':'申请已取消',
            '-4':'取款失败',
        },
        className: 'extractDialog',
        withdrawProposal:'',
        WithdrawalogList:'',
    },
    // 提交查询
    searchSubmit:function(){
        page.submit();
        return false;
    },
    down:function(){
        if(this.tb.list.length===0){
            layer.alert('无记录可下载！');
            return false;
        }
        page.down();
    }
});

vm.$watch('onReady',function(){
    page = new PageModel();
    $("#recordList").on('click','.pno',function(){
        var pno = $(this).attr('data-pno');
        page.extractDetail(pno);
    });
    window['vm'] = vm;
});

function PageModel(){
    this.queryData();
}

// 获取页面数据
PageModel.prototype.queryData = function(){
    Tool.ajax({
        url: '/recode',
        type:"post"
    }).done(function(res){

        var data = res.data,
            accTradeInfoList = data.accountInfo.accTradeInfoList,
            accountList = [];
        accTradeInfoList.forEach(function(v,i) {
            accountList.push({
                val:v.gts2AccountId,
                text: v.accountNo+'['+v.platformName+']',
                currencyName:v.currencyName,
            });  
        });
        vm.accountInfo = data.accountInfo;
        vm.accountDropdown.list = accountList;
        vm.accountDropdown.checked = accTradeInfoList[0].gts2AccountId;
        vm.accountDropdown.checkedText = accTradeInfoList[0].accountNo+'['+accTradeInfoList[0].platformName+']';
        vm.filterData.defaultbTime = latelyDate(0);
        vm.filterData.defaultETime = latelyDate(0);
        vm.platformName = accTradeInfoList[0].platformName;
        page.submit();
    });
}

// 查询
PageModel.prototype.submit = function(){
    var that = this;
    var _params = {
        index : vm.tb.curr,
        pageSize : vm.tb.pageSize,
        beginTime : vm.filterData.defaultbTime,
        endTime : vm.filterData.defaultETime,
        foDisplayStatus : vm.statusData.checked,
        gts2AccountId : vm.accountDropdown.checked,
    };
    if(vm.tb.loading === true) return;
    vm.tb.loading = true;
    vm.tb.errMsg = ''
    Tool.ajax({
        url: '/withdrewRecord',
        data:_params,
        type:"post"
    }).done(function(res){
        vm.tb.loading = false;

        var data = res.data;
        vm.tb.list = data.recordList;
        vm.tb.totalPage = data.totalPage;
        if( data.recordList.length===0 ) vm.tb.errMsg = '查无记录！';
    });
}
// 取款提案详情
PageModel.prototype.extractDetail = function(_id){
    var that = this;
    if(vm.extractDialog.loading === true) return;
    vm.extractDialog.loading = true;
    vm.extractDialog.display = 'block';
    vm.extractDialog.title = _id+'取款详情';
    Tool.ajax({
        url: '/fundDrawingDetail',
        data:{
            proposalNo:_id
        },
        type:"post"
    }).done(function(res){
        vm.extractDialog.loading = false;

        var data = res.data,
            withdrawProposal = data.withdrawProposal;
        if( !withdrawProposal.accountCurrency ) withdrawProposal.accountCurrency = '';
        vm.extractDialog.withdrawProposal = withdrawProposal;
        data.WithdrawalogList.forEach(function(v,i){
            v.settleTimeStr = Tool.timeZone(v.settleTime);
        });
        vm.extractDialog.WithdrawalogList = data.WithdrawalogList;
    });
}
// 取款明细下载
PageModel.prototype.down = function(){
    var that = this;
    var _paramsStr = '?',
        _params = {
            beginTime : vm.filterData.defaultbTime,
            endTime : vm.filterData.defaultETime,
            foDisplayStatus : vm.statusData.checked,
            gts2AccountId : vm.accountDropdown.checked,
        };
    for(var i in _params){
        _paramsStr += i+'='+_params[i]+'&';
    }
    _paramsStr = _paramsStr.replace(/&$/,'');
    location.href = "/api/withdrewRecordDownload.do"+_paramsStr;
}


/**
 * 获取最近N天的日期
 * @param {Number} n 
 */
function latelyDate(n){
    if(isNaN(n)) console.error('n必须为数字');
    var nowdate = new Date(); //获取今天日期
    var oneweekdate = new Date(nowdate-n*24*3600*1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth()+1;
    var d = oneweekdate.getDate();
    m = m<10 ? '0'+m:m;
    d = d<10 ? '0'+d:d;
    return y+'-'+m+'-'+d;
}
/**
 * 获取最近N个月的日期
 * @param {Number} n 
 */
function latelyMonth(n){
    if(isNaN(n)) console.error('n必须为数字');
    var nowdate = new Date(); //获取今天日期
    var nowmonth = nowdate.getMonth();
    nowdate.setMonth(nowmonth-n);
    var y = nowdate.getFullYear();
    var m = nowdate.getMonth()+1;
    var d = nowdate.getDate();
    m = m<10 ? '0'+m:m;
    d = d<10 ? '0'+d:d;
    return y+'-'+m+'-'+d;
}